package com.ecp.commons.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public class RedirectUrlFilter implements Filter {
	//final 

	private static List<String>	list	= new ArrayList<String>();
	static {
		list.add("admin.pinzhi365.com/");
		list.add("admin.pinzhi365.com/index.do");
		list.add("admin.pinzhi365.com/index.jsp");
		list.add("admin.pinzhi365.com/login.do");
		list.add("admin.pinzhi365.com/pinzhiV2.0/productShow.jsp");
		list.add("admin.pinzhi365.com/productShow.do");		
		list.add("admin.pinzhi365.com/pointExt/goods.do");
		list.add("admin.pinzhi365.com/giftIndex.do");
		list.add("admin.pinzhi365.com/saleActiveIndex.do");
		list.add("admin.pinzhi365.com/channelIndex.do");
		list.add("admin.pinzhi365.com/productCategory.do");
		list.add("admin.pinzhi365.com/cartList.do");

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		String serverName = request.getServerName().toLowerCase();
		String requestUri = httpRequest.getRequestURI().toLowerCase();
		String fullUrl = request.getServerName() + httpRequest.getRequestURI();
		if (requestUri.startsWith("/oauseractivating.do")) { // oaUserActivating.do
			String referer = httpRequest.getHeader("Referer");
			//if (referer.toLowerCase().startsWith("http://www.baidu.com/s?")
			if (referer.indexOf("ToReceiveTheGift.aspx") < 0
					&& referer.indexOf("QualificationCertificationInfo.aspx") < 0
					&& referer.indexOf("/vipCenter/centerIndex.do") < 0) {
				httpResponse.sendRedirect("/");
				return;
			}
		}
		if (serverName.startsWith("game.")) {
			if (requestUri.equals("/") || requestUri.equals("/index.do") || requestUri.equals("/index.jsp")) {
				request.getRequestDispatcher("/game/index.do").forward(request, response);
				return;
			}
		} else if (serverName.startsWith("yyg.")) {
			if (requestUri.equals("/") || requestUri.equals("/index.do") || requestUri.equals("/index.jsp")) {
				request.getRequestDispatcher("/yyg/index.do").forward(request, response);
				return;
			}
			if (requestUri.equals("/yyg/index.html")) {
				httpResponse.sendRedirect("/");
				return;
			}
		} else if (serverName.startsWith("www.") && requestUri.startsWith("/yyg/")) {

			StringBuilder sb = new StringBuilder();
			sb.append("http://").append(serverName.replace("www", "yyg")).append(httpRequest.getRequestURI());
			if (StringUtils.isNotBlank(httpRequest.getQueryString())) {
				sb.append("?").append(httpRequest.getQueryString());
			}
			httpResponse.sendRedirect(sb.toString()); // 不是本机要重新定向
			return;
		} else if (serverName.startsWith("www.")
				&& (requestUri.startsWith("/admin/login.jsp") || requestUri.startsWith("/adminLogin.do") || requestUri
						.startsWith("/adminlogin.do"))) {
			StringBuilder sb = new StringBuilder();
			sb.append("http://").append(serverName.replace("www", "admin")).append(httpRequest.getRequestURI());
			if (StringUtils.isNotBlank(httpRequest.getQueryString())) {
				sb.append("?").append(httpRequest.getQueryString());
			}
			httpResponse.sendRedirect(sb.toString()); // 不是本机要重新定向
			return;
		} else if (list.contains(fullUrl)) {
			String url = "http://admin.pinzhi365.com/admin/login.jsp";
			httpResponse.sendRedirect(url); // 不是本机要重新定向
			return;
		}

		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}
